/*
 * Copyright 2010 Eligio Colutta
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package com.google.code.p2pxmppclient;

import java.io.File;
import java.io.IOException;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.PacketFilter;

import com.google.code.p2pxmppclient.notifyserver.NotifyServer;


/**
 * 
 * @author eliosh
 * @version $Rev$
 * @lastautorcommit $Author$
 * @lastdatecommit $Date$
 */
public class P2PXMPPStarter {
	
	public static void main(String[] args) throws IOException{
        String filename = args[0];
    	File f = new File(filename);
    	if(!f.exists()){
    		System.err.println("File not found: " + f.getAbsolutePath());
    		System.exit(1);
    	}
    	File logdir = new File(args[1]);
    	if(!logdir.canWrite()){
    		logdir = new File(System.getProperty("user.dir",".") + "/");
    	}
    	setLog(logdir);
        XMPPConnection connection = Configurer.getConnection(f);        
        PacketFilter filter = Configurer.getFilter();
        connection.addPacketListener(new XMPPPacketListener(connection), filter);
        NotifyServer ns = new NotifyServer(connection);
        ns.startServer();
	}


    private static void setLog(File logDirFile) throws SecurityException, IOException{
    	// String logDir = System.getProperty("user.dir",".") + "/";
    	// File logDirFile = new File(logDir);
    	String logDir = logDirFile.getAbsolutePath() + "/";
    	if(!logDirFile.exists()){
    		logDirFile.mkdirs();
    	}
    	// azzeriamo gli handler predefiniti
    	Handler[] handlers = Logger.getLogger("").getHandlers();
		for(int k=0; k<handlers.length; k++){
			Logger.getLogger("").removeHandler(handlers[k]);
		}
		// creiamo i nuovi handler, uno su file e uno su consolle
		FileHandler fh = new FileHandler(	logDir + "p2pxmppclient.log",
											4194304,
											10,
											true);
		ConsoleHandler ch = new ConsoleHandler();
		
		// logs formatter
        SimpleFormatter sf = new SimpleFormatter();
        
        // assign formatter to the log handlers 
        fh.setFormatter(sf);       
        ch.setFormatter(sf);

        // set defaults
        Logger allLog = Logger.getLogger("");
        allLog.setLevel(Level.WARNING);
        
        Logger.getLogger("com.google.code.p2pxmppclient").setLevel(Level.ALL);

        allLog.addHandler(fh);
        allLog.addHandler(ch);
    }

}
